#!/bin/bash

# TCP网络带宽测试工具 - 依赖安装脚本

echo "TCP网络带宽测试工具 - 依赖安装脚本"
echo "=================================="

# 检测操作系统类型
if [ -f /etc/redhat-release ]; then
    OS="redhat"
    echo "检测到Red Hat系列系统"
elif [ -f /etc/debian_version ]; then
    OS="debian"
    echo "检测到Debian系列系统"
else
    OS="unknown"
    echo "未知操作系统类型"
fi

# 安装C编译器
install_compiler() {
    case $OS in
        "redhat")
            echo "正在安装gcc编译器..."
            if command -v yum >/dev/null 2>&1; then
                yum install -y gcc
            elif command -v dnf >/dev/null 2>&1; then
                dnf install -y gcc
            else
                echo "错误: 未找到包管理器 (yum/dnf)"
                exit 1
            fi
            ;;
        "debian")
            echo "正在安装gcc编译器..."
            apt-get update
            apt-get install -y gcc
            ;;
        *)
            echo "错误: 不支持的操作系统"
            echo "请手动安装gcc编译器"
            exit 1
            ;;
    esac
}

# 检查是否已安装gcc
if command -v gcc >/dev/null 2>&1; then
    echo "gcc编译器已安装: $(gcc --version | head -n1)"
else
    echo "gcc编译器未安装，正在安装..."
    install_compiler
fi

# 验证安装
if command -v gcc >/dev/null 2>&1; then
    echo "✓ gcc编译器安装成功"
    gcc --version | head -n1
else
    echo "✗ gcc编译器安装失败"
    exit 1
fi

# 检查make工具
if command -v make >/dev/null 2>&1; then
    echo "✓ make工具已安装: $(make --version | head -n1)"
else
    echo "make工具未安装，正在安装..."
    case $OS in
        "redhat")
            if command -v yum >/dev/null 2>&1; then
                yum install -y make
            elif command -v dnf >/dev/null 2>&1; then
                dnf install -y make
            fi
            ;;
        "debian")
            apt-get install -y make
            ;;
    esac
fi

echo ""
echo "依赖安装完成！现在可以编译程序了："
echo "  make"
echo ""
